<?php
/**
 * Created by PhpStorm.
 * User: 马超
 * Date: 2017/12/11
 * Time: 16:11
 */

namespace console\controllers;

use app\logic\OrderLogic;
use yii\console\Controller;
use yii\console\Exception;
use Beanstalk\Client;

class BeanstalkController extends Controller
{

    public function actionBeanstalkConsumer(){
        $beanstalk = new Client();
        $beanstalk->connect();
        $beanstalk->watch('order');
        while (true) {
            $job = $beanstalk->reserve(); // Block until job is available.
            // Now $job is an array which contains its ID and body:
            // ['id' => 123, 'body' => '/path/to/cat-image.png']
            // Processing of the job...
            $result = json_decode($job['body'],true);
            $orderLogic = new OrderLogic();
            if($result['type']==1){
                $result = $orderLogic->autoCancel($result['id']);
            }elseif($result['type']==2){
                $result = $orderLogic->autoReceived($result['id']);
            }

            if ($result) {
                $beanstalk->delete($job['id']);
            } else {
                $beanstalk->bury($job['id'],0);
            }
        }
    }


}